/**
 * @date 2:45:03 PM
 * StudentController.java
 * Administrator
 * TODO
 */
package com.qs.education.controllers;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.qs.education.bean.api.PageBean;
import com.qs.education.bean.api.ResponseBean;
import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.beans.EducationStudentResponse;
import com.qs.education.bean.entity.EducationStudent;
import com.qs.education.bean.enums.ResponseStatus;
import com.qs.education.bean.utils.JsonUtil;
import com.qs.education.service.IStudentAPIService;

/**
 * @class StudentController	
 * @author Warren
 * @description
 * @date Oct 31, 2018 
 */
@RestController
public class StudentController {
	
	@Autowired

	private IStudentAPIService studentService;
	
	private final Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * @author Warren
	 * @param request
	 * @param mobile
	 * @param password
	 * @return EducationStudentResponse
	 * @description 根据账号和密码查询学生实体
	 * @date Nov 1, 2018
	 */
	@RequestMapping("/student/getStudentByIdAPI")
	public EducationStudentResponse getStudent(HttpServletRequest request,Long id) {
		log.info("api");
		EducationStudentResponse response = studentService.getEducationStudent(id);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @author Warren
	 * @param educationStudent
	 * @param request
	 * @return ResponseBean
	 * @description 学生注册
	 * @date Nov 6, 2018
	 */
	@RequestMapping("/student/registerAPI")
	public ResponseBean register(EducationStudent educationStudent,HttpServletRequest request) {
		log.info("api");
		ResponseBean response = studentService.register(educationStudent);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @author Warren
	 * @param request
	 * @param mobile
	 * @param password
	 * @return EducationStudentResponse
	 * @description 学生登录
	 * @date Nov 6, 2018
	 */
	@RequestMapping("/student/loginAPI")
	public EducationStudentResponse login(HttpServletRequest request,String mobile,String password) {
		log.info("api");
		EducationStudentResponse response = studentService.login(mobile, password);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @author Warren
	 * @param mobile
	 * @param request
	 * @return ResponseBean
	 * @description 学生退出
	 * @date Nov 6, 2018
	 */
	@RequestMapping("/student/exitAPI")
	public ResponseBean exit(String mobile,HttpServletRequest request) {
		log.info("api");
		ResponseBean response = studentService.exit(mobile);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @author Warren
	 * @param mobile 手机号
	 * @param password 新密码
	 * @return ResponseBean
	 * @description 重置密码
	 * @date Nov 6, 2018
	 */
	@RequestMapping("/student/resetPwdAPI")
	public ResponseBean resetPWD(String mobile,String password,HttpServletRequest request) {
		log.info("api");
		ResponseBean response = studentService.resetPwd(mobile, password);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @author Warren
	 * @param mobile
	 * @return ResponseBean
	 * @description 根据手机号查询是否已注册
	 * @date Nov 7, 2018
	 */
	@RequestMapping("/student/mobileIsRepeatAPI")
	public ResponseBean mobileIsRepeat(String mobile,HttpServletRequest request) {
		log.info("api");
		ResponseBean response = studentService.mobileIsRepeat(mobile);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @author Warren
	 * @param educationStudentJSON
	 * @param file
	 * @param schoolId
	 * @param gradeId
	 * @param classId
	 * @param appAddress
	 * @return ResponseBean
	 * @description 修改学生信息
	 * @date Nov 20, 2018
	 */
	@RequestMapping("/student/updateStudentInfoAPI")
	public ResponseBean updateStudentInfo(String educationStudentJSON,MultipartFile file,String schoolId,String gradeId,String classId,String appAddress) {
		ResponseBean response = studentService.updateStudentInfo(educationStudentJSON, file,schoolId, gradeId, classId, appAddress);
		return response;
	}
	
	/**
	 * @author Warren
	 * @param mobile 手机号
	 * @param oldPassword 原密码
	 * @param newPassword 新密码
	 * @return ResponseBean
	 * @description 修改密码
	 * @date Nov 20, 2018
	 */
	@RequestMapping("/student/changePwdAPI")
	public ResponseBean changePwd(String mobile,String oldPassword,String newPassword){
		ResponseBean responseBean = studentService.changePwd(mobile, oldPassword, newPassword);
		return responseBean;
	}
	
	/**
	 * @author Warren
	 * @param groupId 
	 * @param studentId
	 * @return ResponseBean
	 * @description 学生主动退出班群
	 * @date Nov 20, 2018
	 */
	@RequestMapping("/student/exitGroupAPI")
	public ResponseBean exitGroup(Long groupId,Long studentId){
		ResponseBean responseBean = studentService.exitGroup(groupId, studentId);
		return responseBean;
	}
	
	//---------------------------------------------------------------------------------------
	/**
	 * @Description 查询所有学生
	 * @author Jason
	 * @date Nov 8, 2018
	 * @param request
	 * @return
	 */
	@RequestMapping("/student/getAllStudentAPI")
	public PageBean getStudent(HttpServletRequest request,
			String student,Integer currentPage,Integer pageSize) {

		log.info("api Student 端 Controller  getStudentAPI");
		EducationStudent educationStudent = (EducationStudent) JsonUtil.jsonToBean(student, EducationStudent.class);
		PageBean response = new PageBean();
		response.setToken(response.getToken(request));
		response = studentService.getEducationStudent(educationStudent,currentPage,pageSize);
		response.setStatus(ResponseStatus.SUCCESS.toString());

		return response;
	}
	
	/**
	 * @Description 根据ID删除学生
	 * @author Jason
	 * @date Nov 8, 2018
	 * @param request
	 * @return
	 */
	@RequestMapping("/student/deleteStudentByIdAPI")
	public EducationStudentResponse deleteStudentById(HttpServletRequest request,Long studentId) {
		
		log.info("api Student 端 Controller  deleteStudentById");
		EducationStudentResponse response = new EducationStudentResponse();
		response.setToken(response.getToken(request));
		Boolean result = studentService.deleteStudentyId(studentId);
		if (result) {
			response.setStatus(ResponseStatus.SUCCESS.toString());
		}else {
			response.setStatus(ResponseStatus.FAILED.toString());
		}
		
		return response;
	}
	
	/**
	 * @Description 根据ID查询学生
	 * @author Jason
	 * @date Nov 8, 2018
	 * @param request
	 * @param studentId
	 * @return
	 */
	@RequestMapping("/student/findStudentByIdAPI")
	public EducationStudentResponse findStudentById(HttpServletRequest request,Long studentId) {
		
		log.info("api Student 端 Controller  findStudentById");
		EducationStudentResponse response = new EducationStudentResponse();
		response.setToken(response.getToken(request));
		response = studentService.findStudentById(studentId);
		response.setStatus(ResponseStatus.SUCCESS.toString());
		
		return response;
	}
	
	/**
	 * @Description 修改学生信息
	 * @author Jason
	 * @date Nov 8, 2018
	 * @param request
	 * @param studentId
	 * @return
	 */
	@RequestMapping("/student/updateStudentAPI")
	public EducationStudentResponse updateStudent(HttpServletRequest request, String educationStudent) {
		
		log.info("api Student 端 Controller  updateStudent");
		EducationStudentResponse response = new EducationStudentResponse();
		response.setToken(response.getToken(request));
		EducationStudent student = (EducationStudent)JsonUtil.jsonToBean(educationStudent, EducationStudent.class);
		Boolean result = studentService.updateStudent(student);
		if (result) {
			response.setStatus(ResponseStatus.SUCCESS.toString());
		}else {
			response.setStatus(ResponseStatus.FAILED.toString());
		}
		
		return response;
	}
	

	/**
	 * @Description 增加学生
	 * @author Jason
	 * @date Nov 9, 2018
	 * @param request
	 * @param studentId
	 * @return
	 */
	@RequestMapping("/student/addStudentAPI")
	public EducationStudentResponse addStudent(HttpServletRequest request, String educationStudent) {
		
		log.info("api Student 端 Controller  addStudent");
		EducationStudentResponse response = new EducationStudentResponse();
		response.setToken(response.getToken(request));
		EducationStudent student = (EducationStudent)JsonUtil.jsonToBean(educationStudent, EducationStudent.class);
		Boolean result = studentService.addStudent(student);
		if (result) {
			response.setStatus(ResponseStatus.SUCCESS.toString());
		}else {
			response.setStatus(ResponseStatus.FAILED.toString());
		}
		
		return response;
	}
	
	/**
	 * @Description 根据条件搜索学生
	 * @author Jason
	 * @date Nov 12, 2018
	 * @param request
	 * @param educationStudent
	 * @return
	 */
	@RequestMapping("/student/findStudentByConditionAPI")
	public ResponseListBean<EducationStudent> findStudentByCondition(HttpServletRequest request, String educationStudent) {
		
		log.info("api Student 端 Controller  findStudentByCondition");
		EducationStudent student = (EducationStudent)JsonUtil.jsonToBean(educationStudent, EducationStudent.class);
		ResponseListBean<EducationStudent> responseList = new ResponseListBean<EducationStudent>();
		responseList.setToken(responseList.getToken(request));
		responseList = studentService.findStudentByCondition(student);
		responseList.setStatus(ResponseStatus.SUCCESS.toString());
		
		return responseList;
	}
}
